package com.pocketsweet.ui;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.MLApplication;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLFeedback;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_feedback)
public class FeedBack extends BaseActivity implements OnClickListener {

	private HeaderLayout header;

	@ViewInject(R.id.edtFeedback)
	private static EditText edtFeedback;
	private PackageManager manager;
	private PackageInfo info = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initView();
		setHeaderFunction();
	}

	private void initView() {
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("发送");
		header.setMiddleText("意见反馈");
		header.setRighttContainerClickListener(new onRightContainerListener() {
			@Override
			public void onClick() {
				String feedbackStr = edtFeedback.getText().toString();
				MLFeedback mlFeedback = new MLFeedback();
				mlFeedback.setEmail(UserService.getCurrentUser().getEmail());
				mlFeedback.setAdvise(feedbackStr);
				mlFeedback.setStatus(0);
				mlFeedback.setUser(UserService.getCurrentUser());
				mlFeedback.setSysInfo(android.os.Build.MODEL + " - "
						+ android.os.Build.VERSION.RELEASE);
				manager = FeedBack.this.getPackageManager();
				try {
					info = manager.getPackageInfo(FeedBack.this.getPackageName(), 0);
					mlFeedback.setVersion(info.versionName.toString());
				} catch (PackageManager.NameNotFoundException e) {

					// TODO Auto-generated catch block

					e.printStackTrace();
				}
				mlFeedback.saveInBackground(new SaveCallback() {

					@Override
					public void done(AVException arg0) {
						// TODO Auto-generated method stub
						if (arg0 == null) {
							// Log.e("==成功", "意见反馈成功" + android.os.Build.MODEL
							// + android.os.Build.VERSION.RELEASE);
							edtFeedback.setText("");
							edtFeedback.setHint(getResources().getString(
									R.string.feedback));
							ToolKits.toast(FeedBack.this, "反馈成功");

						} else {
							// Log.e("==error", "意见反馈失败" + arg0.getMessage());
						}
					}
				});

			}
		});
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				FeedBack.this.finish();
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

}
